package com.advertising.controls;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.advertising.biz.IReleaseAddrService;
import com.advertising.model.CloneReleaseAddr;
import com.advertising.model.ReleaseAddr;
import com.tgyt.common.tools.page.Pagination;
import com.tgyt.framework.controls.struts2.BaseTg;

@Scope("prototype")
@Controller(value = "releaseAddrTgControl")
public class ReleaseAddrTg extends BaseTg {

	private Pagination pagination;

	private CloneReleaseAddr releaseAddr;

	private Integer id;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private IReleaseAddrService releaseAddrService;

	public String index() {
		return SUCCESS;
	}

	/**
	 * @Title: getItems
	 * @Description:获得角色列表
	 * @param
	 * @return void
	 * @throws
	 */
	public void getItems() {
		try {
			Pagination pagination = this.releaseAddrService.getPageList(releaseAddrService.convert(releaseAddr)
					, page, rows, sort, order);
			List<CloneReleaseAddr> list =	releaseAddrService.convert(pagination.getList());
			
			 
			System.out.println(JSONArray.fromObject(list).toString());
			outJsonPlainString(response,
					"{\"total\":" + pagination.getTotalCount() + ",\"rows\":"
							+ JSONArray.fromObject(list).toString() + "}");

		} catch (Exception e) {
			e.printStackTrace();
			outJsonPlainString(response, "{\"total\":0, \"rows\":[]}");
		}
	}

	/**
	 * @Title: save
	 * @Description: 保存一条资源记录
	 * @param
	 * @return void
	 * @throws
	 */
	public void save() {
		try {
			this.releaseAddrService.save(releaseAddrService.convert(releaseAddr));
			outJsonPlainString(response, "{\"success\":true}");
		} catch (Exception e) {
			e.printStackTrace();
			outJsonPlainString(response, "{\"error\":true}");
		}
	}

	/**
	 * @Title: update
	 * @Description:修改资源信息
	 * @param
	 * @return void
	 * @throws
	 */
	public void update() {
		try {
			boolean s = this.releaseAddrService.alter(releaseAddrService.convert(releaseAddr));
			// request.setAttribute("allTrees",this.releaseAddrService.getAllHTMLTrees(request));
			outJsonPlainString(response, "{\"success\":true}");
		} catch (Exception e) {
			e.printStackTrace();
			outJsonPlainString(response, "{\"error\":true}");
		}
	}

	/**
	 * @Title: delete
	 * @Description:删除
	 * @param
	 * @return void
	 * @throws
	 */
	public void delete() {
		try {
			this.releaseAddrService.deleteById(id);
			// request.setAttribute("allTrees",this.resourcesService.getAllHTMLTrees(request));
			outJsonPlainString(response, "{\"success\":true}");
		} catch (Exception e) {
			e.printStackTrace();
			outJsonPlainString(response, "{\"error\":true}");
		}
	}

	/**
	 * @Title: find
	 * @Description: 查找一常量
	 * @param
	 * @return void
	 * @throws
	 */
	public void find() {
		ReleaseAddr releaseAddr = this.releaseAddrService.findById(Integer
				.valueOf(id));
		List<ReleaseAddr> list = new ArrayList<ReleaseAddr>();
		if (null != releaseAddr) {
			list.add(releaseAddr);
		}
		JSONArray json = JSONArray.fromObject(list);

		outJsonPlainString(response, "{\"rows\":" + json.toString() + "}");
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

}
